//
//  ViewMoveViewController.m
//  oc_demo
//
//  Created by haigui on 2021/8/27.
//

#import "ViewMoveViewController.h"

@interface ViewMoveViewController ()

@property (nonatomic, strong) UIView *drawView;

@end

@implementation ViewMoveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    _drawView = [[UIView alloc]initWithFrame:CGRectMake(150, 150, 150, 150)];
    _drawView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_drawView];
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(dragViewMoved:)];
    [_drawView addGestureRecognizer:pan];
}

- (void)dragViewMoved:(UIPanGestureRecognizer *)pan{
    
    if (pan.state == UIGestureRecognizerStateChanged) {
        CGPoint translation = [pan translationInView:self.view];
        
        NSLog(@"%@", NSStringFromCGPoint(translation));
        
        _drawView.center = CGPointMake(_drawView.centerX+translation.x, _drawView.centerY+translation.y);
    }
    //关键，不设为零会不断递增，视图会突然不见
    [pan setTranslation:CGPointZero inView:self.view];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
